<?php
/**
 * @param ParamSirtFavorite $ParamSirtFavorite
 */
class ParamSirtFavoritesController extends AppController {

	var $name = 'ParamSirtFavorites';

  //var $uses = array('ParamSirtFavorite', 'ParamSirtBase', 'SirtType');

  // Setup paginate to grab data from the related tables. paginate() is like find(), but provides paginate stuff.
  var $paginate = array('contain' => array('ParamSirtBasis', 
                                           'ParamSirtBasis.SirtType',
                                           'ParamSirtBasis.SirtSubsetModeX', 
                                           'ParamSirtBasis.SirtSubsetModeY'),
                        'limit' => 25
                       );
  /*
   * index()
   */
	function index() {
    
		$this->ParamSirtFavorite->recursive = 0;
    
    /*$this->ParamSirtFavorite->contain('ParamSirtBasis', 
                                      'ParamSirtBasis.SirtType', 
                                      'ParamSirtBasis.SirtSubsetModeX', 
                                      'ParamSirtBasis.SirtSubsetModeY');
     */
    ////////////////////////////////////////////////////////////////////
    ///// Turn the related table data into one logical 'table'. ////////
    $nestedTableData = $this->paginate();  // get the nested table data
    $rows = array();
    
    foreach ($nestedTableData as $intKey => $array) {
    
      // Get the keys from the favs table
      $rows[$intKey]['id']   = $nestedTableData[$intKey]['ParamSirtFavorite']['id'];
      $rows[$intKey]['name'] = $nestedTableData[$intKey]['ParamSirtFavorite']['name'];
      
      // Get the keys from the base table
      foreach($nestedTableData[$intKey]['ParamSirtBasis'] as $baseKey => $baseVal) {
        // first, skip these primary/foreign key fields
        if($baseKey === 'id') { }
        else if( $baseKey === 'sirt_subset_mode_x_id') { }
        else if( $baseKey === 'sirt_subset_mode_y_id') { }
        else if( $baseKey === 'sirt_type_id') { }
        // now deal with the more-nested tables
        else if( $baseKey === 'SirtType' ) {
          $rows[$intKey]['sirt_type'] = $baseVal['name'];  // SirtType.name
        }
        else if($baseKey === 'SirtSubsetModeX' ) {
          $rows[$intKey]['sirt_subset_x'] = $baseVal['name'];  
        }
        else if($baseKey === 'SirtSubsetModeY') {
          $rows[$intKey]['sirt_subset_y'] = $baseVal['name'];  
        }
        // regular base table vals
        else {     
          $rows[$intKey][$baseKey] = $baseVal;
        }
      } //// end inner foreach
    } /////// end outer foreach
    //debug('rows array:');
    //debug($rows);
    
		$this->set('paramSirtFavorites', $this->paginate()); // paginate() is like find('all'), but provides paginate stats
    //$this->set('paramSirtFavorites', $nestedTableData); // paginate() is like find('all'), but provides paginate stats
    
	} /////////////////////// end index() ////////////////////////////////////////
  
  /*
   * view()
   */
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid param sirt favorite', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('paramSirtFavorite', $this->ParamSirtFavorite->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->ParamSirtFavorite->create();
			if ($this->ParamSirtFavorite->save($this->data)) {
				$this->Session->setFlash(__('The param sirt favorite has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The param sirt favorite could not be saved. Please, try again.', true));
			}
		}
		$paramSirtBases = $this->ParamSirtFavorite->ParamSirtBase->find('list');
		$this->set(compact('paramSirtBases'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid param sirt favorite', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->ParamSirtFavorite->save($this->data)) {
				$this->Session->setFlash(__('The param sirt favorite has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The param sirt favorite could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ParamSirtFavorite->read(null, $id);
		}
		$paramSirtBases = $this->ParamSirtFavorite->ParamSirtBase->find('list');
		$this->set(compact('paramSirtBases'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for param sirt favorite', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ParamSirtFavorite->delete($id)) {
			$this->Session->setFlash(__('Param sirt favorite deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Param sirt favorite was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
